<?php
/**
 * Created by PhpStorm.
 * User: frowhy
 * Date: 2017/7/21
 * Time: 下午4:49
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

use Intervention\Image\ImageManagerStatic as Image;

class UploadController extends Controller
{
    use ModelForm;

    /**
     * Storage instance.
     *
     * @var string
     */
    protected $storage = '';
    protected $preUrl  = '';

    protected $useUniqueName = false;

    /**
     * File name.
     *
     * @var null
     */
    protected $name = null;

    /**
     * Upload directory.
     *
     * @var string
     */
    protected $directory = '';

    public function postUploadImg(Request $request)
    {
        if ($request->hasFile('wang-editor-image-file') || $request->hasFile('file')) {
            
            $file     = $request->hasFile('wang-editor-image-file')?$request->file('wang-editor-image-file'):$request->file('file');
            $data     = $request->all();
            $rules    = [
                'wang-editor-image-file' => 'max:5120',
            ];
            $messages = [
                'wang-editor-image-file.max' => '文件过大,文件大小不得超出5MB',
            ];

            $validator = Validator($data, $rules, $messages);
            
            if ($validator->fails()) {
                return 'error|文件过大,文件大小不得超出5MB';
            } else {
                if ($file->isValid()) {
                    $disk = Storage::disk('oss');
                    // 获取文件相关信息
                    $originalName = $file->getClientOriginalName();
                    $ext          = $file->getClientOriginalExtension();
                    $realPath     = $file->getRealPath();
                    $type         = $file->getClientMimeType();
					$name = md5(uniqid());
                    $filename     = $name . '.' . $ext;
                    $filename200     = $name . '_200.' . $ext;
					$file_path = date('Ymd');
					
					$last_name = 'images/'.$file_path.'/'.$filename;
					$last_name200 = 'images/'.$file_path.'/'.$filename200;
					
                    $bool         = $disk->put($last_name, file_get_contents($realPath));
					
                    if ($bool) {
						if($request->hasFile('file'))
						{
							//算取大小
							
							// Image::make($realPath)->resize(200, null, function ($constraint) {$constraint->aspectRatio();})->save(env('UPLOAD_FILE').'/'.$last_name200); 
						}
                        // return Storage::disk('oss')->url($last_name);
                        return env('STATIC_DOMAIN').'/'.$last_name;
                    } else {
                        return 'error|上传失败';
                    }
                } else {
                    return 'error|文件错误';
                }
            }
        }elseif($request->imgurl)
		{
			$disk = Storage::disk('oss');
			$content = @file_get_contents($request->imgurl);
            // dd($content);
			if(!$content)return ['status'=>0, 'message'=>'URL 出错'];
			$name = md5(uniqid());
            $str = strstr($request->imgurl, '?');
            if($str)
            {
                $imgurl = str_replace($str, '', $request->imgurl);
            }else{
                $imgurl = $request->imgurl;
            }
            // dd($imgurl);
			$ext = substr(strrchr($request->imgurl, '.'), 1);
            if(strlen($ext) != 3)
            {
                $ext = 'jpg';
            }
            $filename = $name . '.' . $ext;
			$file_path = date('Ymd');
			$last_name = 'images/'.$file_path.'/'.$filename;
			$bool  = $disk->put($last_name, $content);

			return ['status'=>1, 'message'=>'保存成功', 'src' => env('STATIC_DOMAIN').'/'.$last_name];
		}
    }
}